import re

char_list_10 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
char_list_12 = ['零', '一', '二', '三', '四',
                '五', '六', '七', '八', '九', '十', '十一', '十二']
char_list_24 = char_list_12 + ['十三', '十四', '十五',
                               '十六', '十七', '十八', '十九', '二十', '二十一', '二十二', '二十三']
char_list_31 = char_list_24 + ['二十四', '二十五',
                               '二十六', '二十七', '二十八', '二十九', '三十', '三十一']
char_list_60 = char_list_31[:-2]
for x in '三四五':
    for y in ['']+char_list_10[1:-1]:
        char_list_60.append(x+'十'+y)


def norm_gettext_time(exp_time):
    """
    日-规范化方法：该方法识别时间表达式单元的日字段
    :return:
    """
    # 抽取时间点
    rule = f"({'|'.join(char_list_24)})(点|时)({'|'.join(char_list_60)})*(半|刻|分)?"
    # print(rule)
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)

    rule = f"({'|'.join(char_list_31)})?个?半?小?时?({'|'.join(char_list_60)})?分?钟?之?后"
    # print(rule)
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)

    rule = f"(({'|'.join(char_list_31)})个?刻钟之?后)"
    # print(rule)
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)


def norm_gettext_period(exp_time):
    """
    日-规范化方法：该方法识别时间表达式单元的日字段
    :return:
    """
    # rule = u"((?<!\\d))([0-3][0-9]|[1-9])((日|号)?(早|晚)(晨|上|间)?)"
    rule = u"((上午|下午|中午|晚上|早上|凌晨|清晨|早晨|午后|傍晚|夜晚|晚上))"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)

    rule = u"((每)(上午|下午|中午|晚上|早上|凌晨|清晨|早晨|午后|傍晚|夜晚|晚上))"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)


def norm_gettext_date(exp_time):
    """
    日-规范化方法：该方法识别时间表达式单元的日字段
    :return:
    """
    # rule = u"((?<!\\d))([0-3][0-9]|[1-9])((日|号)?(早|晚)(晨|上|间)?)"
    rule = u"((?<!\\d))([0-3][0-9]|[1-9])((日|号)?)"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)

    # rule = u"((前|昨|今|明|后)(天|日)?(早|晚)(晨|上|间)?)"
    rule = u"((前|昨|今|明|后)(天|日)?)"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)

    exp_time = exp_time.replace('礼拜', '周').replace(
        '星期', '周').replace('每个', '每').replace('周天', '周日')

    rule = u"((每)(天|日))"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)

    rule = u"((每|这个|这|下|下下)?周([一二三四五六日]+)(?!点|时))"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)

    rule = u"((每|这个|这|下|下下)?[((和|还有)?周一)((和|还有)?周二)((和|还有)?周三)((和|还有)?周四)((和|还有)?周五)((和|还有)?周六)((和|还有)?周日)]+(?!点|时))"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)

    rule = u"((每|这个|这|下|下下)?周(一?二?三?四?五?六?日?)到(每|这个|下|下下)?周(一?二?三?四?五?六?日?))"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)

    rule = u"((每|这|下|下下)?个?(周末))"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)
    rule = u"((每|这|下|下下)?个?(工作日))"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    if match:
        print(match[0])
        print(match)


def norm_sethour(exp_time):
    """
        时-规范化方法：该方法识别时间表达式单元的时字段
        :return:
        """
    # rule = u"(?<!(周|星期))([0-2]?[0-9])(?=(点|时))"
    rule = u"(?<!([星期|周]))([0-2]?[0-9])(?=([点|时]))"
    pattern = re.compile(rule)
    match = pattern.search(exp_time)
    print(match[0])
    print(match)


if __name__ == '__main__':
    # exp_time = u'1号早上晚上8点半'
    # exp_time = u'明天早上8点半'
    # exp_time = u'早上8点半'
    # exp_time = u'礼拜一六六点半'
    # exp_time = u'每周一周六六点半'
    # exp_time = u'今天下午八点半'
    # exp_time = u'每个下午八点半'
    exp_time = u'每个下午八点一刻'
    exp_time = u'每个下午八点零一分'
    # exp_time = u'每周五一三下午八点零一分'
    exp_time = u'这周三周一周五下午八点零一分'
    exp_time = u'这周三周一和周五下午八点零一分'
    exp_time = u'这周三周一还有周五下午八点零一分'
    # exp_time = u'二十五分钟后'
    # exp_time = u'一小时二十五分钟后'
    # exp_time = u'二个小时二十五分钟后'
    exp_time = u'一个半小时之后'
    exp_time = u'下午八点一刻'
    # exp_time = u'二十五分钟后'
    # exp_time = u'周一到周五下午八点零一分'
    # exp_time = u'周末下午八点零一分'

    exp_time = exp_time.replace('礼拜', '周').replace(
        '星期', '周').replace('每个', '每').replace('周天', '周日')
    print(exp_time)
    norm_gettext_date(exp_time)
    norm_gettext_period(exp_time)
    norm_gettext_time(exp_time)
    # norm_sethour(u"周1早上8时9点")
    # norm_sethour(u"周1周23时")
